$(function() {
    var i = $(".chapter-list-panel");
    var k = $("#page-chapter-index").val();
    var b = $("#page-id").val();
    var j = Net.navUrlMaping["1"].navigationType;
    var g = App.getQueryParam("code");
    Net.loadUser(App.device(), g, function(o) {
        var l = o.data;
        var m = l.id;
        var n = l.deviceId;
        App.device(n);
        Net.playLog(j, m, b)
    });
    var e = k;
    var f = undefined;
    var d = $("body");
    $(".chapter-body").on("click", function() {
        $(".chapter-top").toggleClass("show-state");
        $(".chapter-footer").toggleClass("show-state");
        $(".chapter-select-panel").removeClass("open-state");
        $(".float-chapter-panel").toggleClass("show-state")
    });
    $(".prev-chapter").on("click", function() {
        if (e == 1) {
            hsycms.fail("没有上一章", function() {}, 1800);
            return
        }
        e--;
        c(e)
    });
    $(".next-chapter").on("click", function() {
        if (e == f) {
            hsycms.fail("没有下一章", function() {}, 1800);
            return
        }
        var m = parseInt(e) + 1;
        var l = $('.chapter-list-panel>[data-index="' + m + '"]');
        App.accessResource(l, function() {
            e++;
            c(e)
        })
    });
    $(".chapter-select").on("click", function() {
        $(".chapter-select-panel").toggleClass("open-state")
    });
    $(".chapter-select-close").on("click", function() {
        $(".chapter-select-panel").toggleClass("open-state")
    });
    $(".chapter-return").on("click", function() {
        history.go(-1)
    });
    $(".chapter-home").on("click", function() {
        App.redirect("main")
    });

    function c(l) {
        h(j, b, l)
    }
    function h(n, m, l) {
        //n = Net.navUrlMaping["1"].navigationType = 1
        //m = $("#page-id").val()
        //l = parseInt($("#page-chapter-index").val());
        Net.chapterContent(n, m, l, function(p) {
            var u = p.data;
            if (App.getUser().vipUser == false) {
                $(".to-recharge-win").attr("data-vip", u.isVip == 1);
                if (u.isVip == 1) {
                    $("body").css({
                        overflow: "hidden"
                    })
                }
            }
            hsycms.closeAll();
            $(".chapter-list-panel>li").removeClass("selected");
            var v = $('.chapter-list-panel>[data-index="' + e + '"]');
            v.addClass("selected");
            var w = v.text();
            $(".chapter-title").html(w);
            var r = $(".chapter-body");
            r.html("");
            var t = u.content;
            var s = JSON.parse(t);
            for (var q = 0; q < s.length; q++) {
                var o = $("<img></img>");
                r.append(o);
                HTML.fillImage(s[q], o, n)
            }
        })
    }
    function a() {
        Net.loadChapterList(j, b, function(l) {
            var p = l.data;
            f = p.length;
            for (var o = 1; o <= f; o++) {
                var m = p[o - 1];
                var n = $('<li data-index="' + m.number + '">' + m.title + "</li>");
                if (m.isVip == 1) {
                    n = $('<li data-vip="1" data-index="' + m.number + '">' + m.title + '<span class="vip"><i class="iconfont icon-VIP"></i></span></li>');
                    n.data("vip", true)
                }
                if (o == e) {
                    n.addClass("selected")
                }
                i.append(n)
            }
            $(".chapter-select-title").html($("#page-novel-title").val() + "- 共" + f + "章");
            h(j, b, parseInt(k))
        })
    }
    a();
    $(".to-recharge-btn").on("click", function() {
        App.redirect("user/pay", {})
    })
});
